package com.qf.bootproducer.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

@Configuration
public class RabbitMQConfig {

    //声明交换机
    @Bean
    public Exchange bootTopicEx(){
        return ExchangeBuilder.topicExchange("boot.topic.ex").durable(true).build();
    }

    //声明队列
    @Bean
    public Queue bootQue1(){
        return QueueBuilder.durable("boot.que1").build();
    }
    @Bean
    public Queue bootQue2(){
        return QueueBuilder.durable("boot.que2").build();
    }

    //绑定关系
    @Bean
    public Binding bindingQue1(Exchange bootTopicEx, Queue bootQue1){
        return BindingBuilder.bind(bootQue1).to(bootTopicEx).with("A.*").noargs();
    }
    @Bean
    public Binding bindingQue2(Exchange bootTopicEx, Queue bootQue2){
        return BindingBuilder.bind(bootQue2).to(bootTopicEx).with("A.#").noargs();
    }


    /******************演示死信*********************/
    @Bean
    public Exchange bootNormalEx(){
        return ExchangeBuilder.directExchange("boot.normal.ex").durable(true).build();
    }
    //声明队列(注意：修改队列的配置一定记得将之前的队列删除)
    @Bean
    public Queue bootNormalQue1(){
        return QueueBuilder
                .durable("boot.normal.que1")
                //设置与当前这个队列对应的死信交换机
                .deadLetterExchange("boot.dead.ex")
                //设置发送到死信交换机的消息对应的路由键
                .deadLetterRoutingKey("dead.key")
                //设置队列的容量
                .maxLength(5)
                //设置该队列消息的超时时间(10s)
                .ttl(10000)
                .build();
    }
    @Bean
    public Binding bindingQue3(Exchange bootNormalEx, Queue bootNormalQue1){
        return BindingBuilder.bind(bootNormalQue1).to(bootNormalEx).with("A.b").noargs();
    }

    //声明死信交换机
    @Bean
    public Exchange bootDeadEx(){
        return ExchangeBuilder.directExchange("boot.dead.ex").durable(true).build();
    }
    //声明与死信交换机关联的死信队列
    @Bean
    public Queue bootDeadQue1(){
        return QueueBuilder.durable("boot.dead.que").build();
    }
    //绑定死信交换机和死信队列的关系
    @Bean
    public Binding bindingQue4(Exchange bootDeadEx, Queue bootDeadQue1){
        return BindingBuilder.bind(bootDeadQue1).to(bootDeadEx).with("dead.key").noargs();
    }

    /***************************演示惰性队列****************************/
    @Bean
    public Queue bootLazyQue(){
        return QueueBuilder
                .durable("boot.lazy.que")
                //设置该队列为惰性队列，发送到该队列的消息存储在磁盘，消费时才会加载回内存
                .lazy()
                .build();
    }

    @Bean
    public Binding bindingQue5(Exchange bootNormalEx, Queue bootLazyQue){
        return BindingBuilder.bind(bootLazyQue).to(bootNormalEx).with("A.lazy").noargs();
    }

    /*******************演示幂等问题*********************/
    @Bean
    public Queue bootMidengQue(){
        return QueueBuilder
                .durable("boot.mideng.que")
                .build();
    }

    @Bean
    public Binding bindingQue6(Exchange bootNormalEx, Queue bootMidengQue){
        return BindingBuilder.bind(bootMidengQue).to(bootNormalEx).with("A.mideng").noargs();
    }
}
